package com.shuai.jit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuai.jit.entity.user.User;
import com.shuai.jit.entity.vo.VoUser;
import com.shuai.jit.mapper.vo.VoUserMapper;
import com.shuai.jit.service.UserService;
import com.shuai.jit.mapper.UserMapper;
import com.shuai.jit.utils.RandomString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    VoUserMapper voUserMapper;
    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @Override
    public Integer updateUser(User user) {

        if(user.getId()==null){
            return -3;
        }
        // 因为电话号码和用户名是 唯一的
        User temp=this.baseMapper.selectByTelephone(user.getUserTelephone());


        // 用户的手机号是唯一的
        if(temp==null){
            log.info("手机号不存在更新用户");
            return this.baseMapper.updateById(user);
        }
        log.info(temp.toString());
        // 更改是手机真好是
        if(temp.getId().equals(user.getId())){
            log.info("手机号存在,更新用户");
            return this.baseMapper.updateById(user);
        }
        return -2;

    }

    @Override
    public Integer updatePassword(User user) {
        log.info(user.toString());
        // 密码是否为空
        if(StringUtils.isEmpty(user.getPassword())){
            return -1;
        }
        User temp=this.baseMapper.selectById(user.getId());
        if(temp!=null){
            String salt= RandomString.getRandomString(10);
            temp.setSalt(salt);
            BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
            temp.setPassword(passwordEncoder.encode(user.getPassword()+salt));
            return this.baseMapper.updateById(temp);
        }
        return -1;
    }



    @Override
    public User getUserInfo(String userId) {
        return this.baseMapper.getUserInfo(userId);
    }

    public Integer checkUser(User user){
        // 准备认证
        user.setApprove(1);
        if(user.getLevName().equals("学生")){
            user.setUserLev(1);
        }
        if(user.getLevName().equals("教师")){
            user.setUserLev(2);
        }
        return this.baseMapper.updateById(user);
    }


    public Integer updateCheck(String id,Integer approve){

        User user=this.baseMapper.selectById(id);
        if(user!=null){
            user.setApprove(approve);
            return  this.baseMapper.updateById(user);
        }else {
            return -1;
        }

    }

    @Override
    public Page<VoUser> getCheckUser(Integer current) {
        Page<VoUser> page=new Page<>(current,8);
        QueryWrapper<VoUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("approve",1);
        return voUserMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Page<User> getAllUser(Integer current) {
        Page<User> page=new Page<>(current,8);
        return this.baseMapper.selectPage(page,null);
    }

    @Override
    public Integer addUser(User user) {

        if(user==null||user.getUserTelephone()==null||user.getUsername()==null){
            return -2;
        }
        User temp=this.baseMapper.selectRegister(user.getUserTelephone(),user.getUsername());
        if(temp!=null){
            return -1;
        }

        String salt= RandomString.getRandomString(10);
        user.setSalt(salt);
        user.setUserLev(0);

        if(user.getLevName()!=null){
            if(user.getLevName().equals("学生")){
                user.setUserLev(1);
            }
            if(user.getLevName().equals("教师")){
                user.setUserLev(2);
            }
        }

        // 刚注册用户为 0
        user.setApprove(2);
        user.setUserIntroduce("新用户，啥也没写");
        user.setUserImg("https://jokerak.oss-cn-beijing.aliyuncs.com/avatar");
        BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
        user.setPassword(passwordEncoder.encode(user.getPassword()+salt));
        return this.baseMapper.insert(user);

    }
}
